﻿(function ($) {
    /*----------------------------------*/
    $.BrowerType = {};
    $.BrowerType.MSIE = "MSIE";
    $.BrowerType.EDGE = "edge";
    $.BrowerType.FireFox = "FireFox";
    $.BrowerType.Opera = "Opera";
    $.BrowerType.Chrome = "Chrome";
    $.BrowerType.Safari = "Safari";

    $.Brower = function (type, ver) {
        this.Type = type;
        this.Version = ver;
        this.toString = function () {
            return "[" + this.Type + ", " + this.Version + "]";
        }
    }

    $.Brower.GetBrowserInfo = function () {
        var s;
        var _bt = {};
        var b = navigator.userAgent.toLowerCase();

        (s = b.match(/edge\/([\d.]+)/)) ? _bt.edge = s[1] :
        (s = b.match(/firefox\/([\d.]+)/)) ? _bt.firefox = s[1] :
        (s = b.match(/opera.([\d.]+)/)) ? _bt.opera = s[1] :
        (s = b.match(/opr.([\d.]+)/)) ? _bt.opera = s[1] :
        ((s = b.match(/msie ([\d.]+)/)) || (s = b.match(/rv:([\d.]+)/))) ? _bt.ie = s[1] :
        (s = b.match(/chrome\/([\d.]+)/)) ? _bt.chrome = s[1] :
        (s = b.match(/version\/([\d.]+).*safari/)) ? _bt.safari = s[1] : 0;

        if (_bt.edge) return new $.Brower($.BrowerType.EDGE, _bt.edge);
        if (_bt.ie) return new $.Brower($.BrowerType.MSIE, _bt.ie);
        if (_bt.firefox) return new $.Brower($.BrowerType.FireFox, _bt.firefox);
        if (_bt.chrome) return new $.Brower($.BrowerType.Chrome, _bt.chrome);
        if (_bt.opera) return new $.Brower($.BrowerType.Opera, _bt.opera);
        if (_bt.safari) return new $.Brower($.BrowerType.Safari, _bt.safari);

        return null;
    }

    $.Brower.Current = $.Brower.GetBrowserInfo();

    $.StringToDate = function (value) {
        if (value instanceof Date) {
            return value;
        } else if (typeof (value) === "string") {
            if (/\/Date\(\d.*\)\//.test(value)) {
                return new Date(parseInt(value.match(/\d+/)[0]));
            } else {
                switch ($.Brower.Current.Type) {
                    case $.BrowerType.EDGE:
                    case $.BrowerType.Chrome:
                        if (value.indexOf('T') > 0) {
                            var s = value.split('T');
                            try {
                                if (s.length == 1) {
                                    return new Date(s[0]);
                                } else if (s.length == 2) {
                                    return new Date(s[0] + " " + s[1]);
                                }
                            } catch (e) {
                                return new Date(Date.parse(s[0] + " " + s[1]));
                            }
                        } else {
                            try {
                                return new Date(value);
                            } catch (e) {
                                return new Date(Date.parse(value));
                            }
                        }
                        break;

                    case $.BrowerType.MSIE:
                        try {
                            return new Date(value);
                        } catch (e) {
                            return new Date(Date.parse(value));
                        }

                    default:
                        try {
                            return new Date(value);
                        } catch (e) {
                            return new Date(Date.parse(value));
                        }
                }
            }
        } else {
            try {
                return new Date(value);
            } catch (e) {
                return new Date(Date.parse(value));
            }
        }
    }

    $.GetTimestamp = function (v) {
        if (v instanceof Date) {
            return v.getTime();
        } else {
            return new Date().getTime();
        }
    }

})(jQuery);


if (!String.prototype.endsWith) {
    String.prototype.endsWith = function (s) {
        if (s != null) {
            if (this.length >= s.length) {
                if (s.length > 0) {
                    var i = s.length - 1;
                    var j = this.length - 1;
                    for (; i <= 0; i--, j--) {
                        if (this[j] != s[i]) {
                            break;
                        }
                    }
                    if (i < 0) { return true; }
                }
            }
        }

        return false;
    };
}

